package com.redoop.db;

import com.redoop.main.MainProc;
import org.apache.ibatis.session.SqlSession;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

public class ColumMgr {

    public Map<String,Object> getColumJson(){
        Connection conn = null;
        DatabaseMetaData dbmd = null;
        SqlSession session = MainProc.gDBCtrl.getSQLSession();
        Map<String,Object> map = new HashMap<>();
        try {
            conn = session.getConnection();
            dbmd = conn.getMetaData();
            List<String> list = MainProc.gDBCtrl.getTableNameList();
            for (String readLine: list) {
                ResultSet rs = dbmd.getColumns(null, null, readLine, null);
                String DataXQ = getDataXQ(readLine);
                String DataXH = "],\"writeMode\": \"append\",\"fieldDelimiter\": \"\\t\",}}}]}}";
                HashSet<String> h = new LinkedHashSet<>();
                while (rs.next()) {

                    String columName = rs.getString("COLUMN_NAME");
                    String columType = rs.getString("TYPE_NAME");

                    if(columType.equals("VARCHAR2")|columType.equals("VARCHAR")| columType.equals("CHAR")| columType.equals("NUMBER")){
                        columType="string";
                    }else if (columType.equals("TIMESTAMP")){
                        columType="Date";
                    }
                    // String sql=columnane+" "+columtype+","+"\n";//拼接sql使用
                    String json = "{\"name\":\"" + columName + "\",\"type\":\"" + columType + "\"},";// 拼接dataX的json文件使用
                    h.add(json);
                    // bw.write(json + "\n");

                    // bw.write(columnane+" "+columtype+"\n");

                }
                String DataXJ="";
                for (String string : h) {
                    DataXJ=DataXJ+string;
                    //sbw.write(DataXQ+string+DataXH + "\n");
                }
                String result = DataXQ+DataXJ+DataXH;
                map.put(readLine,result);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;

    }

    public String getDataXQ(String tableName){
        String jdbcType = "";
        String jdbcUserName = "";
        String jdbcPassWord = "";
        String DataBase = "";
        String readLine = tableName;
        String jdbcURL = "";
        String hdfsURL ="default";
        String savePath = "default";
        FileReader fileReader = null;
        BufferedReader br = null;

        try {
            fileReader = new FileReader(MainProc.gIniFile.getJdbc_path());
            br = new BufferedReader(fileReader);
            Properties prop = new Properties();
            prop.load(br);
            if("mysqlreader".equals(MainProc.gDBCtrl.getJdbcType())){
                jdbcType="mysqlreader";
                jdbcUserName = prop.getProperty("mysql.username","root");;
                jdbcPassWord= prop.getProperty("mysql.password","root");
                DataBase=prop.getProperty("mysql.database","sakila");
                jdbcURL= prop.getProperty("mysql.jdbcURL","jdbc:mysql://localhost:3306/sakila?characterEncoding=utf-8&amp;useSSL=false&amp;autoReconnect=true");
            }
            hdfsURL = prop.getProperty("hdfsURL","default");
            savePath = prop.getProperty("savepath","default");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileReader.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        String DataXQ = "{\"job\": {\"setting\": {\"speed\": {\"channel\":1}},\"content\": [{\"reader\": {\"name\": \""
                + jdbcType + "\",\"parameter\": {\"username\": \"" + jdbcUserName + "\",\"password\": \""
                + jdbcPassWord
                + "\",\"connection\": [{\"querySql\": [\"select * from "+DataBase+"."+readLine+"\"],\"jdbcUrl\": [\""
                + jdbcURL + "\",]}]}},\"writer\": {\"name\": \"hdfswriter\",\"parameter\": {\"defaultFS\": \""
                + hdfsURL + "\",\"fileType\": \"text\",\"path\": \"" + savePath
                + "\",\"fileName\": \""+readLine+"\",\"column\": [";

        return DataXQ;
    }
}
